package com.kaluli.project.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kaluli.project.domain.entity.KllMainOrder;
import com.kaluli.project.domain.entity.KllOrderSynApi;
import com.kaluli.project.domain.entity.KllProductVo;
import com.kaluli.project.domain.repository.KllMainOrderRepository;
import com.kaluli.project.domain.repository.KllOrderSynApiRepository;
import com.kaluli.project.service.KllCornApiService;
import com.kaluli.project.service.KllDataService;
import com.kaluli.project.service.KllMainOrderService;
import com.kaluli.project.service.KllOrderLogisticsService;
import com.kaluli.project.service.KllOrderSynApiService;
import com.kaluli.project.service.KllProductService;
import com.kaluli.project.utils.MD5Utils;
import com.kaluli.project.utils.RegexUtil;
import com.kaluli.project.utils.nanshaxml.XmlFanli;

/**
 * 
 * 外部接口
 * 
 * @author niwei
 * 
 */
@Controller
@PropertySource({ "classpath:config.properties","classpath:thridsystem.properties" })
public class CronApiController {

	private static Logger log = Logger.getLogger(CronApiController.class);

	@Autowired
	private Environment env;

	@Autowired
	private KllOrderSynApiService kllOrderSynApiService;
	
	@Autowired
	private KllMainOrderRepository kllMainOrderRepository;

	@Autowired
	private KllOrderSynApiRepository kllOrderSynApiRepository;

	@Autowired
	private KllMainOrderService kllMainOrderService;
	
	@Autowired
	private KllCornApiService kllCornApiService;
	
	@Autowired
	private KllDataService kllDataService;
	
	@Autowired
	private KllOrderLogisticsService kllOrderLogisticsService;


	@RequestMapping(value = "/item/stock")
	@ResponseBody
	public ModelAndView findStockNum() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			kllCornApiService.findStockNum();
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	

	/**
	 * 用户数据日报
	 * @return
	 */
	@RequestMapping(value = "/user/data")
	@ResponseBody
	public ModelAndView findUserData() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			kllCornApiService.findUserData();
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	
	/**
	 * 用户数据周报
	 * @return
	 */
	@RequestMapping(value = "/user/week")
	@ResponseBody
	public ModelAndView findUserDataWeek() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			kllCornApiService.findUserDataWeek();
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	
	/**
	 * 物流信息周报
	 * @return
	 */
	@RequestMapping(value = "/logistic/week")
	@ResponseBody
	public ModelAndView findLogisticByWeek() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			kllDataService.findByWeek();
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	
	/**
	 * cps
	 * @return
	 */
	@RequestMapping(value = "/order/cps")
	@ResponseBody
	public ModelAndView findByCps() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			kllCornApiService.findCpsUser();
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	
	
	/**
	 * 速尔物流信息同步(10分钟一次)
	 * @return
	 */
//	@RequestMapping(value = "/sr/logistic")
//	@ResponseBody
	public ModelAndView optLogistic() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			
			kllOrderLogisticsService.optLogistic(null);
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	
	/**
	 * 速尔物流信息同步(上午九点,下午四点)
	 * @return
	 */
//	@RequestMapping(value = "/sr/updAbroad")
//	@ResponseBody
	public ModelAndView updateLogistic() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			
			kllOrderLogisticsService.optLogistic("sr");
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	
	/**
	 * 能容订单重推机制
	 * @return
	 */
	@RequestMapping(value = "/nr/post")
	@ResponseBody
	public ModelAndView checkNrpost() {
		Map<String, Object> map = new HashMap<>();
		String code = "";
		String message = "";
		try {
			
			kllOrderSynApiService.checkNrresult();
			code = "0";
			message = "";
		} catch (Exception e) {
			code = "3";
			message = e.getMessage();
		}
		map.put("code", code);
		map.put("message", message);
		return new ModelAndView(new MappingJacksonJsonView(), map);
	}
	


	
}
